/**
 * 使用两个线程来累加 count 的值
 * 每个线程循环 1w 次，累加变量 count 的值，count 默认值为 0，注意线程安全问题。
 */

class Counter {
    private int count = 0;
    void add(){
        synchronized(this){
            count++;
        }
    }
    public int get(){
        return count;
    }
}

public class demo8 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        Thread thread1 = new Thread(() ->{
            for (int i = 0;i <5000;i++){
                counter.add();
            }
        });

        Thread thread2= new Thread(() ->{
            for (int i = 0;i <5000;i++){
                counter.add();
            }
        });
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println("count="+counter.get());
    }




}
